package com.sduept.nwld.dataserver.manager.cim;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.dto.LineparamDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 线路参数管理类
 * 
 * @author Xiaopy
 *
 */
@Service
public class LineparamDTOManager {

	@Autowired
	private SuperDAO dao;

	/**
	 * 查询所有线路
	 * 
	 * @return
	 */
	public List<LineparamDTO> findAllLinerparams() {
		return getLineparamByCondition(null);
	}

	/**
	 * 根据名字查找 交流电路TA
	 * 
	 * @param name
	 * @return
	 */
	public List<LineparamDTO> getLineparamByName(String name) {
		return getLineparamByCondition("name = '" + name + "'");
	}

	/**
	 * 更新t_imp_lineparams 的TA TV
	 */
	public void updateLineparam(LineparamDTO dto) {
		dao.createOrUpdate(dto, LineparamDTO.class);
	}

	/**
	 * 根据名 字母线 和厂站 查询交流线路TA （ t_imp_lineparams）
	 * 
	 * @param lineparamName       名字
	 * @param lineparamBus        母线
	 * @param lineparamSubStation 厂站
	 * @return
	 */
	public List<LineparamDTO> getLineparamByNameAndBusAndSubStation(String lineparamName, String lineparamBus,
			String lineparamSubStation, Float selectVoltage) {
		String sql = "";
		if (lineparamName != null && !lineparamName.isEmpty()) {
			sql += "and name like '%" + lineparamName + "%' ";
		}
		if (lineparamBus != null && !lineparamBus.isEmpty()) {
			sql += "and bus like '%" + lineparamBus + "%' ";
		}
		if (lineparamSubStation != null && !lineparamSubStation.isEmpty()) {
			sql += "and substation like '%" + lineparamSubStation + "%' ";
		}
		if (selectVoltage != null) {
			sql += "and ems_id in (select a.id from ACLineSegment a where a.standVoltagelevel = " + selectVoltage + " )";
		}
		if ("".equals(sql)) {
			return new ArrayList<LineparamDTO>();
		} else {
			sql = sql.substring(3, sql.length());
			return getLineparamByCondition(sql);
		}
	}

	/**
	 * 根据变电站名称和名称 查找线路参数
	 * 
	 * @param name
	 * @param stationName
	 * @return
	 */
	public List<LineparamDTO> listLineparamDTOsByNameAndStationName(String name, String stationName) {
		List<LineparamDTO> rtn = dao.findByCondition(LineparamDTO.class, "name=? and substation=?", name, stationName);
		return rtn;
	}

	/**
	 * 根据变电站名称和名称 查找线路参数
	 * 
	 * @param name
	 * @param stationName
	 * @return
	 */
	public List<LineparamDTO> listLineparamDTOsByEidAndStationId(String eid, String stationId) {
		List<LineparamDTO> rtn = dao.findByCondition(LineparamDTO.class, "ems_id=? and substation_id=?", eid,
				stationId);
		return rtn;
	}

	/**
	 * 条件查询交流线路TA （ t_imp_lineparams）
	 * 
	 * @param where
	 * @return
	 */
	public List<LineparamDTO> getLineparamByCondition(String where) {
		List<LineparamDTO> rtn = dao.findByCondition(LineparamDTO.class, where, new Object[0]);
		return rtn;

	}

	/**
	 * 根据线路Id 查找
	 * 
	 * @param id
	 * @return
	 */
	public List<LineparamDTO> getLineparamByEms_id(String id) {
		return getLineparamByCondition(" ems_id = " + id);
	}

	/**
	 * 新建
	 * 
	 * @param dto
	 */
	public void createLineparam(LineparamDTO dto) {
		dao.createOrUpdate(dto, LineparamDTO.class);
//		String sql = "insert into t_imp_lineparams (name,substation,substation_id,TA,TV,TA_P,TA_S,TV_P,TV_S,ems_id) " + "values('" + dto.getName() + "','" + dto.getSubstation() + "','"
//				+ dto.getSubstation_id() + "','" + dto.getTA() + "','" + dto.getTV() + "','" + dto.getTA_P() + "','" + dto.getTA_S() + "','" + dto.getTV_P() + "','" + dto.getTV_S() + "','"
//				+ dto.getEms_id() + "')";
//		dao.nativeUpdate(sql);
	}

}
